package mq.service.impl;

import com.alibaba.fastjson.JSON;
import common.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import mq.service.IMqMailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * @description: Mq消息异常邮件预警
 * @author: zk
 * @since: 2022-08-16 14:11
 */
@Slf4j
@Service
public class MqMailServiceImpl implements IMqMailService {
    public static final Integer TYPE_CONFIRM_CALL_BACK = 1, TYPE_RETURN_CALL_BACK = 2, TYPE_MESSAGE_CUMULATE = 3;
    public static final String MAIL_CONFIRM_CALL_BACK = "MAIL_CONFIRM_CALL_BACK";
    public static final String MAIL_RETURN_CALL_BACK = "MAIL_RETURN_CALL_BACK";
    public static final String MAIL_MESSAGE_CUMULATE = "MAIL_MESSAGE_CUMULATE";

    @Resource
    private RedisUtils redisUtils;
    //@Resource
    //private MailServiceUtil mailServiceUtil;
    @Value("${mail.toEmil}")
    private String toEmil;

    @Override
    public void addRecord(String messageId, Integer type) {
        log.info("消息异常邮件预警-添加记录 入参:messageId={},type={}",messageId,type);
        String mailPrefix = getMailPrefix(type);
        if (StringUtils.isBlank(mailPrefix)) {
            return;
        }
        String recordStr = redisUtils.get(this.getClass(), mailPrefix, String.class);
        Set<String> recordSet = StringUtils.isNotBlank(recordStr) ?
                JSON.parseObject(recordStr, HashSet.class) : new HashSet<>();
        // 只缓存50条
        if(recordSet.size() <= 50) {
            recordSet.add(messageId);
            redisUtils.set(this.getClass(),mailPrefix, JSON.toJSONString(recordSet));
        }
    }

    // 根据type获取缓存前缀
    private String getMailPrefix(Integer type) {
        String mailPrefix = StringUtils.EMPTY;
        switch (type) {
            case 1: mailPrefix = MAIL_CONFIRM_CALL_BACK;break;
            case 2: mailPrefix = MAIL_RETURN_CALL_BACK;break;
            case 3: mailPrefix = MAIL_MESSAGE_CUMULATE;break;
        }
        return mailPrefix;
    }

    @Override
    public void sendMail() {
        String confirmRecordStr = redisUtils.get(this.getClass(), MAIL_CONFIRM_CALL_BACK, String.class);
        String returnRecordStr = redisUtils.get(this.getClass(), MAIL_RETURN_CALL_BACK, String.class);
        String cumulateRecordStr = redisUtils.get(this.getClass(), MAIL_MESSAGE_CUMULATE, String.class);

        if (StringUtils.isNotBlank(confirmRecordStr)
                || StringUtils.isNotBlank(returnRecordStr)
                || StringUtils.isNotBlank(cumulateRecordStr)) {
            // 立即邮件预警
            /*
            mailServiceUtil.sendMail(toEmil, "MQ发送消息异常异常, 消费者到交换机失败记录=" + confirmRecordStr + ";" +
                            "交换机到队列失败记录=" + returnRecordStr + ";" +"消费者处理异常记录="  +cumulateRecordStr +"。" +
                            "详情见表mq_message_abnormal、mq_message_cumulate, 请尽快核对处理。",
                    "【MQ异常记录预警】");
              */
            // 删除
            redisUtils.del(this.getClass(),MAIL_CONFIRM_CALL_BACK);
            redisUtils.del(this.getClass(),MAIL_RETURN_CALL_BACK);
            redisUtils.del(this.getClass(),MAIL_MESSAGE_CUMULATE);
        }

    }
}
